#include "Exception.h"
#include "SvrBase.h"

#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif // _WIN32_WINNT

#ifdef _DEBUG
#pragma warning(disable:4127)		// conditional expression is constant
#endif

#pragma warning(disable:4786)		// disable "identifier was truncated to 'number' characters in the debug information"
#pragma warning(disable:4290)		// C++ Exception Specification ignored
#pragma warning(disable:4097)		// typedef-name 'identifier1' used as synonym for class-name 'identifier2'
#pragma warning(disable:4001)		// nonstandard extension 'single line comment' was used
#pragma warning(disable:4100)		// unreferenced formal parameter
#pragma warning(disable:4699)		// Note: Creating precompiled header 
#pragma warning(disable:4710)		// function not inlined
#pragma warning(disable:4514)		// unreferenced inline function has been removed
#pragma warning(disable:4512)		// assignment operator could not be generated
#pragma warning(disable:4310)		// cast truncates constant value

#include <eh.h>

#pragma warning(push, 3) 
#include <exception>
#pragma warning(pop) 

#include <stdlib.h>
#include <crtdbg.h>
#include "ExTranslator.h"

using namespace sbase;

//////////////////////////////////////////////////////////////////////////
Exception::Exception(EXCEPTION_POINTERS * pExp) : m_pExp(pExp)
{
}

//////////////////////////////////////////////////////////////////////////
const char* Exception::GetName() const
{
#define EXCEPTION_NO_MEMORY STATUS_NO_MEMORY
    static char buf [11] = {'0', 'x',0,};
    
	switch (m_pExp->ExceptionRecord->ExceptionCode)
	{
		case EXCEPTION_NO_MEMORY:					return "No Memory";
		case EXCEPTION_ACCESS_VIOLATION :			return "Access Violation";
		case EXCEPTION_DATATYPE_MISALIGNMENT :		return "Datatype Misalignment";
		case EXCEPTION_BREAKPOINT :					return "Breakpoint";
		case EXCEPTION_SINGLE_STEP :				return "Single Step";
		case EXCEPTION_ARRAY_BOUNDS_EXCEEDED :		return "Array Bounds Exceeded";
		case EXCEPTION_FLT_DENORMAL_OPERAND :		return "Float Denormal Operand";
		case EXCEPTION_FLT_DIVIDE_BY_ZERO :			return "Float Divide by Zero";
		case EXCEPTION_FLT_INEXACT_RESULT :			return "Float Inexact Result";
		case EXCEPTION_FLT_INVALID_OPERATION :		return "Float Invalid Operation";
		case EXCEPTION_FLT_OVERFLOW :				return "Float Overflow";
		case EXCEPTION_FLT_STACK_CHECK :			return "Float Stack Check";
		case EXCEPTION_FLT_UNDERFLOW :				return "Float Underflow";
		case EXCEPTION_INT_DIVIDE_BY_ZERO :			return "Integer Divide by Zero";
		case EXCEPTION_INT_OVERFLOW :				return "Integer Overflow";
		case EXCEPTION_PRIV_INSTRUCTION :			return "Privileged Instruction";
		case EXCEPTION_IN_PAGE_ERROR :				return "In Page Error";
		case EXCEPTION_ILLEGAL_INSTRUCTION :		return "Illegal Instruction";
		case EXCEPTION_NONCONTINUABLE_EXCEPTION :	return "Noncontinuable Exception";
		case EXCEPTION_STACK_OVERFLOW :				return "Stack Overflow";
		case EXCEPTION_INVALID_DISPOSITION :		return "Invalid Disposition";
		case EXCEPTION_GUARD_PAGE :					return "Guard Page";
		case EXCEPTION_INVALID_HANDLE :				return "Invalid Handle";
		case 0xE06D7363 :							return "Microsoft C++ Exception";
		default :									return _itoa(m_pExp->ExceptionRecord->ExceptionCode,buf+2,sizeof(buf)-2-1);
	};			
}

//////////////////////////////////////////////////////////////////////////
EXCEPTION_POINTERS* 
Exception::GetExceptionInfo(void)
{
	return m_pExp;
}


//////////////////////////////////////////////////////////////////////////
void*  sbase::TransInit(void)
{	
	CExTranslator* pTrans = new CExTranslator;
	return pTrans ? (void*)pTrans : NULL;
}

//////////////////////////////////////////////////////////////////////////
void sbase::TransFini(void* pTrans)
{
	CExTranslator* pTranslator = (CExTranslator*)pTrans;
	SAFE_DELETE(pTranslator);
}	

